/**
 * Created with IntelliJ IDEA.
 * User: "Balasubramanian"
 * Date: 8/8/13
 * Time: 1:55 PM
 * To change this template use File | Settings | File Templates.
 */

/*
A library object represents a collection of books.
BibliotecaApp uses this library object internally to store its books and their statuses.
The books are initially stored in a text file, that is passes as input to the constructor of BooksLibrary object.
The library object reads the books and puts it in a HashMap in memory.
The books are identified by their id.
 */

package com.twu.biblioteca;


import java.io.*;
import java.util.*;
import java.util.logging.Logger;

public class BooksLibrary {

    private HashMap<String,Book> books;
    private String FREE="YES";
    private Logger logger;

    private String IN_USE="NO";
    private String SEPARATOR=" ";
    private String SOURCE_FILE="";
    private String ERROR_MESSAGE="Sorry,the system has encountered an error. Please try again later";


    //constructor
    public BooksLibrary(String srcFilePath, Logger logger)
    {
        this.logger=logger;
        SOURCE_FILE=srcFilePath;
        books=new HashMap<>();
        readBooksFromFile();
    }


    // Reads books from the file specified in the filePath and stores it in the HashMap
    private void readBooksFromFile()
    {
        ArrayList<String> linesFromFile=UtilityFunctions.readAllLinesFromFile(SOURCE_FILE,logger);
        for(String input:linesFromFile)
        {
            String[] fields=input.split(SEPARATOR);
            Book book=new Book(fields[0],fields[1],fields[2]);
            books.put(fields[0],book);

        }
    }

    // Checks if the book is available for borrowing
	public boolean isBookAvailable(String id)
    {
        Book book=books.get(id);
        if(book==null || book.getAvailability().equals(IN_USE))
            return false;
        return true;
    }


    // This method sets the availability status of a book to YES
    public void checkIn(String id)
    {
        Book book=books.get(id);
        if(book==null)
            return;
        book.setAvailability(FREE);
    }

    // This method sets the availability status of a book to NO
    public void checkOut(String id)
    {
        Book book=books.get(id);
        if(book==null)
            return;
        book.setAvailability(IN_USE);
    }

    // Checks if the book with given id is present in library
    public boolean hasBook(String id)
    {
        return (books.get(id)!=null);
    }


    // This method returns all the books in the library
    public ArrayList<Book> getAllBooks()
    {
        ArrayList<Book> res=new ArrayList<>();
        for(Map.Entry<String,Book> e : books.entrySet())
        {
            res.add(e.getValue());
        }
        return res;
    }


    // Save tbe book list to the source file
    public void saveToFile()
    {
        try(PrintWriter writer=new PrintWriter(new FileWriter(SOURCE_FILE)))
        {
            for(Map.Entry<String,Book> e:books.entrySet())
            {
                Book book=e.getValue();
                writer.println(book.getId()+SEPARATOR+book.getName()+SEPARATOR+book.getAvailability());
            }
        }catch(IOException e){
           logger.severe(e.getMessage());
           System.out.println(ERROR_MESSAGE);
           System.exit(1);
        }

    }

}